package com.by.service.impl;

import com.by.bean.params.PurchaseQuery;
import com.by.bean.pucharse.Purchase;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.exception.BusinessException;
import com.by.mapper.ContractMapper;
import com.by.mapper.PurchaseDetailMapper;
import com.by.mapper.PurchaseMapper;
import com.by.service.PurchaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
public class PurchaseServiceImpl extends BaseServiceImpl<Purchase, PurchaseMapper> implements PurchaseService {

    String currentMaxCode;//当前最大编码  CG2024001

    @Autowired
    PurchaseDetailMapper purchaseDetailMapper;

    @Autowired
    ContractMapper contractMapper;


    @Transactional
    @Override
    public int update(Purchase entity) {
        //更新采购单
        int rs = baseMapper.update(entity);
        if (rs == 0)
            throw new BusinessException("更新采购单失败");
        //更新明细
        //先删除之前的明细
        purchaseDetailMapper.deleteByPurchaseId(entity.getId());
        //生成明细
        rs = purchaseDetailMapper.batchSave(entity.getId(), entity.getPurchaseDetailList());
        if (rs == 0)
            throw new BusinessException("更新采购明细失败");

        if (null != entity.getContracts() && entity.getContracts().size() > 0) {
            //先删除合同
            contractMapper.deleteByPurchaseId(entity.getId(), 1);
            //更新合同
            rs = contractMapper.batchSave(1, entity.getId(), entity.getContracts());
            if (rs == 0)
                throw new BusinessException("更新合同失败");
        }

        return 1;
    }

    @Transactional
    @Override
    public int save(Purchase entity) {
        entity.setCode(generateCode());
        //保存采购单
        int rs = super.save(entity);
        if (rs == 0)
            throw new BusinessException("保存采购单失败");
        //保存采购明细
        rs = purchaseDetailMapper.batchSave(entity.getId(), entity.getPurchaseDetailList());
        if (rs == 0)
            throw new BusinessException("保存采购单明细失败");
        //保存采购合同
        if (null != entity.getContracts() && entity.getContracts().size() > 0) {
            rs = contractMapper.batchSave(1, entity.getId(), entity.getContracts());
            if (rs == 0)
                throw new BusinessException("合同附件保存失败");
        }

        return 1;
    }

    /**
     * 生成新的编码
     *
     * @return
     */
    String generateCode() {
        String year = getYear();
        //判断当前最大编码是否存在
        if (null == currentMaxCode) {
            //如果不存在  查询当年最大编码
            currentMaxCode = baseMapper.queryMaxCode(year);
            if (null == currentMaxCode) {
                //说明不存在 生产当前第一个编码
                currentMaxCode = "CG" + year + "001";
            } else {
                //如果存在 新编码序号+1
                getNextCode();
            }
        } else {
            //如果存在 新编码序号+1
            getNextCode();
        }

        return currentMaxCode;
    }

    /**
     * 得到新编码
     *
     * @return
     */
    void getNextCode() {
        //CG2024001
        String prefix = currentMaxCode.substring(0, 6);//前缀
        String seq = currentMaxCode.substring(6);
        //转换为数字 001  tmp=1
        int tmp = Integer.parseInt(seq);
        tmp++;
        currentMaxCode = prefix + String.format("%03d", tmp);
    }

    /**
     * 获取年
     *
     * @return
     */
    String getYear() {
        LocalDate localDate = LocalDate.now();
        return String.valueOf(localDate.getYear());
    }
    /**
     * 查询采购单列表
     * @param query
     * @return
     */
    @Override
    public PageBean search(PurchaseQuery query) {
        PageHelper.startPage(query.getPage(),query.getPageSize());
        List list=baseMapper.search(query);
        PageInfo info=new PageInfo<>(list);
        return new PageBean<>(query.getPage(),query.getPageSize(),info.getTotal(),list);
    }
}
